import { createI18n } from 'vue-i18n'

import zhCn from './zhCn'
import zhTw from './zhTw'
import en from './en'

const messages = {
  zhCn: {
    ...zhCn,
  },
  zhTw: {
    ...zhTw,
  },
  en: {
    ...en,
  }
}

const getCurrentLanguage = () => {
  const chooseLanguage = localStorage.getItem('language')
  if (chooseLanguage) return chooseLanguage
  let langCode = ''
  switch (navigator.language.toLowerCase()) {
    case "zh-tw":
      langCode = 'zhTw'
      break;
    case "zh-hk":
      langCode = 'zhTw'
      break;
    case "zh-cn":
      langCode = 'zhCn'
      break;
    default:
      langCode = 'en'
      break;
  }
  localStorage.setItem('language', langCode)
  return langCode
}

const i18n = createI18n({
  legacy: false,
  globalInjection: true,
  locale: getCurrentLanguage() || 'zhCn',
  messages
})

export default i18n